// Uber shader fragment program.
uniform vec4 agtk_ambientColor;
uniform vec4 agtk_diffuseColor;
uniform vec4 agtk_falloffColor;
uniform float agtk_specularIntensity;
uniform float agtk_specularPower;
uniform float agtk_glowIntensity;
uniform float agtk_falloffIntensity;
uniform float agtk_falloffPower;
uniform float agtk_falloffDetail;
uniform sampler2D agtk_diffuseMap;
uniform sampler2D agtk_normalMap;
uniform sampler2D agtk_specularMap;
uniform sampler2D agtk_glowMap;

varying vec2 Texcoord;
varying vec3 ViewDirection;
varying vec3 LightDirection;
varying float FalloffValue;

void main( void )
{
   vec4 diffuseMapColor = texture2D(agtk_diffuseMap, Texcoord );
   vec4 normalMapColor = texture2D(agtk_normalMap, Texcoord );
   vec4 specularMapColor = texture2D(agtk_specularMap, Texcoord );
   vec4 glowMapColor = texture2D(agtk_glowMap, Texcoord );
   
   vec4 ambientColor = agtk_ambientColor;
   vec4 diffuseColor = agtk_diffuseColor;
   vec4 falloffColor = agtk_falloffColor;
   
   vec3 fragLightDirection = normalize( LightDirection );
   vec3 fragNormal = normalize( ( normalMapColor.xyz * 2.0 ) - 1.0 );
   float fragNDotL = dot( fragNormal, fragLightDirection );
   
   float falloffDetail = agtk_falloffDetail*((1-abs(dot(fragNormal, vec3(0,0,1))))*2-0.8);
   float falloffCompile = agtk_falloffIntensity*(falloffDetail+FalloffValue);
   falloffCompile/=agtk_falloffPower;
   
   vec3 fragReflection = normalize( ( ( 2.0 * fragNormal ) * fragNDotL ) - fragLightDirection ); 
   vec3 fragViewDirection = normalize( ViewDirection );
   float fragRDotV = max( 0.0, dot( fragReflection, fragViewDirection ) );
   
   vec4  TotalAmbient = ambientColor * diffuseMapColor; 
   vec4  TotalDiffuse = diffuseColor * fragNDotL * diffuseMapColor; 
   vec4  TotalSpecular = agtk_specularIntensity * specularMapColor * ( pow( fragRDotV, agtk_specularPower ) );
   vec4  TotalGlow = vec4(glowColor.x*agtk_glowIntensity, glowColor.y*agtk_glowIntensity, glowColor.z*agtk_glowIntensity, 1);
   vec4  TotalFalloff = falloffColor*falloffCompile;
     
   vec4  TotalColor = (TotalAmbient + TotalDiffuse + TotalSpecular + TotalGlow + TotalFalloff);
  
   gl_FragColor = TotalDiffuse;
}